local Msg = require("core.Msg")
local ActDefine = require("activity.Define")
local ComplexSrvActExcel = require("excel.ComplexSrvActivity")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local ChongzhiRankExcel = ComplexSrvActExcel.chongzhiRank
local Human = require("core.Human")
local MailLogicFW =require("mail.MailLogicFW")
local Config = require("Config")
local ComplexSrvActFW = require("activity.ComplexSrvActFW")

RECHARGE_DBDATA = RECHARGE_DBDATA or nil
RECHARGE_RANK = RECHARGE_RANK or {}

function init()
    RECHARGE_DBDATA = ComplexSrvActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
end

ExcludeUserList = {}
ExcludeUserList[100028] = {"尹剑身", "阳佟不凡", "家子敬", "许艳一", "禹乱", "种凡", "伊忘幽", "郗博", "史铁身", "鲍欣雪"}
ExcludeUserList[100030] = {"唐鹤", "贲真", "鲍仇天", "雷豁"}
ExcludeUserList[100031] = {"李冷颜", "刘冬", "金琦", "皇甫芘", "爱优璇"}
ExcludeUserList[100036] = {"诸不正", "谭慧音"}
ExcludeUserList[100037] = {"满茗茶", "干罡"}
ExcludeUserList[100038] = {"牧柳", "谷汝燕"}
ExcludeUserList[100050] = {"云冷"}
ExcludeUserList[100072] = {"夏侯文舒","萧百招","窦戎","周公节","小二","太史静","中奖了","嘻嘻哈","舞纶欣","司空淑颖","昌欣雪"}
ExcludeUserList[100073] = {"涵儿","舞莹","灰机"}
ExcludeUserList[100074] = {"无剑","萌面超人","任伯苗","潘夜阑","军淑颖"}
ExcludeUserList[100075] = {"孔晴","小猪哥"}
ExcludeUserList[100076] = {"赵绝悟"}


function onRecharge(humanuuid, humanname, rechargeNum, nosave, init)
    if not ComplexSrvActFW.onCheckActIsRunning(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK) then
        return
    end

    if not RECHARGE_DBDATA then
        return
    end

    if not init then
        if RECHARGE_DBDATA._status then
            return
        end
    end

    if not RECHARGE_RANK then
        return
    end

    if not init then
        if RECHARGE_DBDATA[humanuuid] then
            RECHARGE_DBDATA[humanuuid][2] = (RECHARGE_DBDATA[humanuuid][2] or 0) + rechargeNum 
        else
            RECHARGE_DBDATA[humanuuid] = {humanname, rechargeNum}
        end
    end

    if not nosave then
        ComplexSrvActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, RECHARGE_DBDATA)
    end

    local nRechargeNum = RECHARGE_DBDATA[humanuuid][2]
    local nRuleID = ComplexSrvActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if nRuleID and ChongzhiRankExcel[nRuleID] then
        local nChongZhiRankConfig = ChongzhiRankExcel[nRuleID]
        if nRechargeNum < nChongZhiRankConfig.lessrecharge[4] then
            return
        end

        if ExcludeUserList and ExcludeUserList[Config.SVR_INDEX] then
            for k, v in pairs(ExcludeUserList[Config.SVR_INDEX]) do
                if v == humanname then
                    return
                end
            end
        end
    else
        return
    end

    local nBeforeRank = 0
    local nCurRank = 0

    if #RECHARGE_RANK == 0 then
        onAddRankList(RECHARGE_RANK, 1, humanuuid, humanname, nRechargeNum)
    else
        for k,v in ipairs(RECHARGE_RANK) do
            if v[1] == humanname then
                nBeforeRank = k
                break
            end
        end

        if nBeforeRank == 1 then
            RECHARGE_RANK[1][2] = nRechargeNum 
            nCurRank = 1
        else
            if nBeforeRank > 0 and nRechargeNum <= RECHARGE_RANK[nBeforeRank - 1][2] then
                RECHARGE_RANK[nBeforeRank][2] = nRechargeNum 
                nCurRank = nBeforeRank 
            else --增加积分后排名发生变化
                local nBegin = 0
                if nBeforeRank > 0 then --之前已有排名则先删除
                    table.remove(RECHARGE_RANK, nBeforeRank)
                    nBegin = nBeforeRank - 1
                else
                    nBegin = #RECHARGE_RANK 
                end
                if nRechargeNum <= RECHARGE_RANK[nBegin][2] then
                    onAddRankList(RECHARGE_RANK, nBegin + 1, humanuuid, humanname, nRechargeNum)
                    nCurRank = nBegin + 1
                else
                    for k = nBegin, 1, -1 do
                        if k == 1 then
                            if nRechargeNum <= RECHARGE_RANK[k][2] then 
                                onAddRankList(RECHARGE_RANK, k + 1, humanuuid, humanname, nRechargeNum)
                                nCurRank = k + 1
                                break
                            else
                                onAddRankList(RECHARGE_RANK, 1, humanuuid, humanname, nRechargeNum)
                                nCurRank = 1
                                break
                            end
                        else
                            if nRechargeNum <= RECHARGE_RANK[k][2] then 
                                onAddRankList(RECHARGE_RANK, k + 1, humanuuid, humanname, nRechargeNum)
                                nCurRank = k + 1
                                break
                            end
                        end
                    end
                end
            end
        end
    end
    onProecss(Human.onlineUuid[humanuuid], humanuuid, 1)
end

function onCalc()
    local nRuleID = ComplexSrvActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if nRuleID and ChongzhiRankExcel[nRuleID] then
        local nChongZhiRankConfig = ChongzhiRankExcel[nRuleID]
        local nRewardConfigList = nChongZhiRankConfig.rewardlist
        if nRewardConfigList then
            for k, v in ipairs(RECHARGE_RANK) do
                local nRewardData = nil
                local nNotEnought = 0
                local nRewardList = {}
                local nRealRank = 0
                if k <= 3 then
                    for i = k, 3 do
                        if v[2] >= ChongzhiRankExcel[nRuleID].lessrecharge[i] then
                            if k < i then
                                nNotEnought = 1
                            end
                            nRewardData = nRewardConfigList[i]
                            nRealRank = i
                            break
                        end
                    end
                    if not nRewardData then
                        nRealRank = 4
                        nRewardData = nRewardConfigList[4]
                    end
                else
                    nRewardData = nRewardConfigList[4]
                end
                if nRewardData then
                    nRewardList[#nRewardList + 1] = get_grid(nRewardData[1], nRewardData[2], nRewardData[3], 1)
                end
                local nContent = string.format("恭喜您在合服活动【充值排行】活动中荣获第%d名，成功获得以下奖励!", k)
                if k <= 3 and nNotEnought == 1 then
                    nContent = string.format("恭喜您在合服活动【充值排行】中获得了第%d名（但充值金额未达到%d元宝），将顺次领取第%d名奖励。希望你在下次的活动中再接再厉！", k, ChongzhiRankExcel[nRuleID].lessrecharge[k], nRealRank)
                    
                end
                MailLogicFW.addSystemMessage(v[3], "充值排行奖励", nContent, nRewardList)
            end
        end
    end
    if RECHARGE_DBDATA then
        RECHARGE_DBDATA._status = true 
        ComplexSrvActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, RECHARGE_DBDATA)
    end
end

function onActEnd()
    RECHARGE_DBDATA = nil
    RECHARGE_RANK = nil
    ComplexSrvActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, RECHARGE_DBDATA)
end

function onActRunning()
    RECHARGE_DBDATA = ComplexSrvActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if not RECHARGE_DBDATA then
        RECHARGE_DBDATA = {}
    end
    RECHARGE_RANK = {} 
    for k, v in pairs(RECHARGE_DBDATA) do
        if type(v) == "table" then
            onRecharge(k, v[1], v[2], true, true)
        end
    end
end

function get_grid(itemID, itemNum, itemColor, itemBind)
	local grid = Grid.create(nil, itemID, itemNum, itemBind, ItemDefine.qualityOut[itemColor])
	return grid
end

function onProecss(human, humanuuid)
    local nSendMsg = Msg.gc.GC_COMPLEXSRVACT_CHONGZHIRANK_QUREY
    nSendMsg.rewardranklist[0] = 0
    nSendMsg.myranklist[0] = 0
    local nRuleID = 0
    nRuleID = ComplexSrvActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if not nRuleID or not ChongzhiRankExcel[nRuleID] then
        return
    end

    nSendMsg.lessrecharge[0] = 0
    for k, v in ipairs(ChongzhiRankExcel[nRuleID].lessrecharge) do
        nSendMsg.lessrecharge[0] = nSendMsg.lessrecharge[0] + 1 
        nSendMsg.lessrecharge[nSendMsg.lessrecharge[0]] = v
    end

    local nChongZhiRankConfig = ChongzhiRankExcel[nRuleID]
    local nRewardList = nChongZhiRankConfig.rewardlist
    for k, v in ipairs(nRewardList) do
        nSendMsg.rewardranklist[0] = nSendMsg.rewardranklist[0] + 1
        local nData = nSendMsg.rewardranklist[nSendMsg.rewardranklist[0]]
        wrapData(nData, k)
        nData.reward[0] = 1
        local grid = get_grid(v[1], v[2], nil, 0)
        Grid.makeItem(grid, nData.reward[1])
        if k >= 4 then
            break
        end
    end

    if RECHARGE_RANK then
        local nMyRank = 0
        for k, v in ipairs(RECHARGE_RANK) do
            if v[3] == humanuuid then
                nMyRank = k 
                break
            end
        end

        if nMyRank > 0 then
            local nBeforeRank = nMyRank - 1
            if RECHARGE_RANK[nBeforeRank] then
                nSendMsg.myranklist[0] = nSendMsg.myranklist[0] + 1
                local nData = nSendMsg.myranklist[nSendMsg.myranklist[0]]
                wrapData(nData, nBeforeRank)
                nData.reward[0] = 0
            end

            nSendMsg.myranklist[0] = nSendMsg.myranklist[0] + 1
            local nData = nSendMsg.myranklist[nSendMsg.myranklist[0]]
            wrapData(nData, nMyRank)
            nData.reward[0] = 0

            local nLastRank = nMyRank + 1
            if RECHARGE_RANK[nLastRank] then
                nSendMsg.myranklist[0] = nSendMsg.myranklist[0] + 1
                local nData = nSendMsg.myranklist[nSendMsg.myranklist[0]]
                wrapData(nData, nLastRank)
                nData.reward[0] = 0
            end

            if nMyRank == 1 then
                if RECHARGE_RANK[nMyRank + 2] then
                    nSendMsg.myranklist[0] = nSendMsg.myranklist[0] + 1
                    local nData = nSendMsg.myranklist[nSendMsg.myranklist[0]]
                    wrapData(nData, nMyRank + 2)
                    nData.reward[0] = 0
                end
            end
        end
    end
    if human then
        Msg.send(nSendMsg, human.fd)
    end
end

function wrapData(nData, rank)
    nData.rank = rank
    nData.rechargenum = 0
    nData.humanname = ""
    if RECHARGE_RANK then
        nData.rechargenum = RECHARGE_RANK[rank] and RECHARGE_RANK[rank][2] or 0
        nData.humanname = RECHARGE_RANK[rank] and RECHARGE_RANK[rank][1] or ""
    end
end

function onAddRankList(rechargeRank, oRank, humanuuid, humanname, rechargeNum)
    table.insert(rechargeRank, oRank, {humanname, rechargeNum, humanuuid})
end

function onChangeRankeName(humanUuid, humanName)
    if RECHARGE_RANK then
        for k, v in ipairs(RECHARGE_RANK) do
            if v[3] == humanUuid then
                v[1] = humanName
            end
        end
    end
end

ComplexSrvActFW.onRegisterCallBack(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, nil, onActRunning, onActEnd, nil)
